package com.mdlib.helper.module.account.fragments;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.MDAppUtils;
import com.mdlib.helper.utils.ToastUtil;
import com.orhanobut.logger.Logger;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by librabin on 16/11/24.
 */

public class ForgetSecondFragment extends BaseTitleFragment {

    @Bind(R.id.et_forget_pass)
    EditText mNewPwdField;
    @Bind(R.id.et_forget_pass_again)
    EditText mConfirmPwdField;

    @Bind(R.id.tv_forget_ok)
    TextView mTvForgetOk;

    private String mPhoneNum;

    public static ForgetSecondFragment newInstance(String phoneNum) {
        ForgetSecondFragment fragment = new ForgetSecondFragment();
        Bundle args = new Bundle();
        args.putString(UIHelper.EXTRA_PHONE_NUM, phoneNum);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initBundle(Bundle bundle) {
        super.initBundle(bundle);
        mPhoneNum = bundle.getString(UIHelper.EXTRA_PHONE_NUM);
        Logger.e(mPhoneNum);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_forget_steptwo;
    }


    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.forget_tv_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
    }

    @OnClick(R.id.tv_forget_ok)
    public void onClick() {
        String newPwd = mNewPwdField.getText().toString();
        String confirmPwd = mConfirmPwdField.getText().toString();
        if (!newPwd.equals(confirmPwd)) {
            ToastUtil.showToast("两次输入密码不一致");
        }

        if (!MDAppUtils.checkPassword(newPwd)) {
            return;
        }

        setNewPwd(mPhoneNum, newPwd);

    }


    private void setNewPwd(String phoneNum, String newPwd) {
        addSubscribe(DataManager.getInstance().getAccountApi()
                .setNewPwd(newPwd, phoneNum)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        addFragment(ForgetThreeFragment.newInstance());
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));


    }


}
